﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using UnitTestUtils.UI;
using System.Windows.Navigation;
using System.Windows.Controls;
using System.Windows.Media;
using System.Timers;
using System.Windows.Threading;
using System.Windows;

namespace UnitTestUtilsTests.UI
{
    [TestFixture]
    public class UIHostTest
    {
        Timer _timer;

        /// <summary>
        /// Write your test story here.
        /// </summary>
        [TearDown]
        public void Dispose()
        {
        }

        [Test, RequiresSTA]
        public void Control_ShowDialog()
        {
            var wnd = new Control();
            wnd.Background = Brushes.AliceBlue;

            UIHost target = UIHost.Create(wnd);

            target.FinalizeInitialization();
            scheduleClose(target.Window);

            target.ShowDialog();
        }

        [Test, RequiresSTA]
        public void Control_bring_your_own_window()
        {
            var wnd = new Window();
            wnd.Background = Brushes.AliceBlue;

            UIHost target = UIHost.Create(wnd);

            target.FinalizeInitialization();
            scheduleClose(target.Window);

            target.ShowDialog();
        }

        [Test, RequiresSTA]
        public void Control_WaitForClose()
        {
            var wnd = new Control();
            wnd.Background = Brushes.AliceBlue;

            var target = UIHost.Create(wnd);

            target.FinalizeInitialization();
            scheduleClose(target.Window);

            target.Show();

            target.WaitForClose();
        }

        [Test, RequiresSTA]
        public void Page_ShowDialog()
        {
            var wnd = new Page();
            wnd.Background = Brushes.AliceBlue;

            UIHost target = UIHost.Create(wnd);

            target.FinalizeInitialization();
            scheduleClose(target.Window);

            target.ShowDialog();
        }

        [Test, RequiresSTA]
        public void Page_WaitForClose_working_with_Page()
        {
            var wnd = new Page();
            wnd.Background = Brushes.AliceBlue;

            var target = UIHost.Create(wnd);

            target.FinalizeInitialization();
            scheduleClose(target.Window);

            target.Show();

            target.WaitForClose();
        }

        [Test, RequiresSTA]
        public void WindowLoaded_hook_is_called()
        {
            var wnd = new Control();
            wnd.Background = Brushes.AliceBlue;
            
            UIHost target = UIHost.Create(wnd);

            bool hookCalled = false;
            target.WindowLoaded = () => hookCalled = true;

            target.FinalizeInitialization();
            scheduleClose(target.Window, 2000);

            target.ShowDialog();

            Assert.IsTrue(hookCalled);
        }

        #region Internals
        private void scheduleClose(Window window)
        {
            scheduleClose(window, 500);
        }

        private void scheduleClose(Window window, double timeToWait)
        {
            _timer = new Timer();
            _timer.Interval = timeToWait;

            _timer.Elapsed += (s, a) =>
                {
                    _timer.Stop();
                    _timer.Dispose();

                    window.Dispatcher.BeginInvoke((Action)(() =>
                        {
                            window.Close();
                        }));
                };

            _timer.Start();
        } 
        #endregion
    }
}
